﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Sys_RoleManage : BasePage
    {
        private Sys_RoleBLL srb = new Sys_RoleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "ROLE_ID";
                ViewState["OrderDire"] = " desc ";
                BindData(true);
                //BindGridView();
            }
        }

        /// <summary>
        /// 绑定GridView数据源
        /// </summary>
        ////public void BindGridView()
        ////{
        ////    lblRID.Value = "";
        ////    txtRoleName.Text = "";
        ////    ddlRoleState.SelectedValue = "1";
        ////    DataTable dt = srb.GetAllInfo();

        ////    gvRole.DataSource = dt;
        ////    gvRole.DataBind();
        ////}
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string strCon = "";
                if (txtRoleName.Text != "")
                {
                    strCon += txtRoleName.Text;
                }
                strCon += ",";
                if (ddlRoleState.SelectedValue != "-1")
                {
                    strCon += ddlRoleState.SelectedValue;
                }

                ViewState["Where"] = strCon;
                BindData(false);
            }
            catch (Exception)
            {
                lblMes.Text = "连接数据库失败，请联系管理员！";
                //MessageBox.Show(Page, "连接数据库失败，请联系管理员！！！");
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtRoleName.Text.Trim()))
                {
                    lblMes.Text = "角色名不能为空！";
                    //MessageBox.Show(Page, "角色名不能为空");
                    return;
                }

                string name = srb.GetRoleName(txtRoleName.Text.Trim());
                if (txtRoleName.Text.Trim() == name)
                {
                    lblMes.Text = "角色名不能重复添加！";
                    return;
                }
                Sys_Role model = new Sys_Role();

                model.ROLE_NAME = txtRoleName.Text;
                model.ROLE_STATE = ddlRoleState.SelectedValue;

                int row = srb.INSERT(model, base.lognUser.USER_NAME);
                if (row > 0)
                {
                    //MessageBox.Show(Page, "添加成功");
                    lblMes.Text = "添加成功！";
                    BindData(false);
                }
                else
                {
                    lblMes.Text = "添加失败！";
                    //MessageBox.Show(Page, "添加失败");
                }
            }
            catch (Exception)
            {
                lblMes.Text = "连接数据库失败，请联系管理员！";
                //MessageBox.Show(Page, "连接数据库失败，请联系管理员！！！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtRoleName.Text.Trim()))
                {
                    lblMes.Text = "角色名不能为空！";
                    //MessageBox.Show(Page, "角色名不能为空");
                    return;
                }
                Sys_Role model = new Sys_Role();

                model.ROLE_ID = decimal.Parse(lblRID.Value);
                model.ROLE_NAME = txtRoleName.Text;
                model.ROLE_STATE = ddlRoleState.SelectedValue;

                int row = srb.UPDATE(model, base.lognUser.USER_NAME);
                if (row > 0)
                {
                    //MessageBox.Show(Page, "修改成功");
                    lblMes.Text = "修改成功！";
                    BindData(false);
                    //Response.AddHeader("Refresh", "0");
                }
                else
                {
                    //MessageBox.Show(Page, "修改失败");
                    lblMes.Text = "修改失败！";
                }
            }
            catch (Exception)
            {
                lblMes.Text = "连接数据库失败，请联系管理员！";
                //MessageBox.Show(Page, "连接数据库失败，请联系管理员！！！");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelele_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(lblRID.Value))
                {
                    lblMes.Text = "请选择要删除的项！";
                    //MessageBox.Show(Page, "请选择要删除的项");
                    return;
                }
                int row = srb.DELETE(lblRID.Value, base.lognUser.USER_NAME);
                if (row > 0)
                {
                    lblMes.Text = "删除成功！";
                    //MessageBox.Show(Page, "删除成功");
                    BindData(false);
                    //Response.AddHeader("Refresh", "0");
                }
                else
                {
                    lblMes.Text = "删除失败！";
                    //MessageBox.Show(Page, "删除失败");
                }
            }
            catch (Exception)
            {
                lblMes.Text = "连接数据库失败，请联系管理员！";
                //MessageBox.Show(Page, "连接数据库失败，请联系管理员！！！");
            }
        }

        /// <summary>
        /// gvRole_PageIndexChanging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvRole.PageIndex = e.NewPageIndex;
                BindData(false);
                TextBox tb = (TextBox)gvRole.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (gvRole.PageIndex + 1).ToString();
            }
            catch { }
        }

        /// <summary>
        /// gvRole_RowCommand
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "go")
            {
                try
                {
                    TextBox tb = (TextBox)this.gvRole.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    gvRole_PageIndexChanging(null, ea);
                }
                catch { }
            }
        }

        /// <summary>
        /// gvRole_SelectedIndexChanging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string ids = ((Label)gvRole.Rows[e.NewSelectedIndex].FindControl("lblRoleId")).Text.ToString();
            Sys_Role model = srb.GetInfoById(ids);

            lblRID.Value = model.ROLE_ID.ToString();
            txtRoleName.Text = model.ROLE_NAME;
            ddlRoleState.SelectedValue = model.ROLE_STATE;
        }

        /// <summary>
        /// Render
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow row in gvRole.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(gvRole, "Select$" + row.RowIndex.ToString(), true);
                    row.Attributes["style"] = "cursor:pointer";
                    row.Attributes["title"] = "单击选择行";
                }
            }

            base.Render(writer);
        }

        /// <summary>
        /// GridView 标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        //public void BindProjectName(bool iSsort)
        //{
        //    string strCon = "";
        //    strCon += txtRoleName.Text + ",";
        //    strCon += ddlRoleState.SelectedValue;

        //    DataTable dt = srb.GetInfoByCondition("Condition", strCon);

        //    DataView view = dt.DefaultView;
        //    if (iSsort)
        //    {
        //        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        //        view.Sort = sort;//设置排序
        //    }

        //    gvRole.DataSource = view;

        //    gvRole.DataBind();
        //}

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool isFirst)
        {
            var Type = "";
            string strCon = "";
            try
            {
                if (!string.IsNullOrEmpty(ViewState["Where"].ToString()))
                {
                    strCon = ViewState["Where"].ToString();
                    if (strCon != ",")
                    {
                        Type = "Condition";
                    }
                    else
                    {
                        Type = "";
                    }
                }
            }
            catch (Exception)
            {
                Type = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = srb.GetRecordCount(Type, strCon);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = srb.GetListByPage(Type, strCon, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = gvRole;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = gvRole;
                gvRole.DataSource = null;
                gvRole.DataBind();
                PageIng.Visible = false;
            }
        }
    }
}